package com.newegg.datafeed.adserving.adwords;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder;
import com.google.api.ads.adwords.axis.v201809.cm.AdGroup;
import com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdRotationMode;
import com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation;
import com.google.api.ads.adwords.axis.v201809.cm.AdGroupPage;
import com.google.api.ads.adwords.axis.v201809.cm.AdGroupReturnValue;
import com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface;
import com.google.api.ads.adwords.axis.v201809.cm.AdGroupStatus;
import com.google.api.ads.adwords.axis.v201809.cm.AdRotationMode;
import com.google.api.ads.adwords.axis.v201809.cm.ApiException;
import com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration;
import com.google.api.ads.adwords.axis.v201809.cm.Bids;
import com.google.api.ads.adwords.axis.v201809.cm.CpcBid;
import com.google.api.ads.adwords.axis.v201809.cm.CriterionTypeGroup;
import com.google.api.ads.adwords.axis.v201809.cm.Money;
import com.google.api.ads.adwords.axis.v201809.cm.Operator;
import com.google.api.ads.adwords.axis.v201809.cm.Selector;
import com.google.api.ads.adwords.axis.v201809.cm.Setting;
import com.google.api.ads.adwords.axis.v201809.cm.TargetingSetting;
import com.google.api.ads.adwords.axis.v201809.cm.TargetingSettingDetail;
import com.google.api.ads.adwords.lib.selectorfields.v201809.cm.AdGroupField;
import com.google.api.ads.common.lib.exception.ValidationException;

public class ADGroupService extends ADWordsParent{
	AdGroupServiceInterface adGroupService;
	
	public ADGroupService(String customerId) throws ValidationException {
		adGroupService = get(customerId, AdGroupServiceInterface.class);
	}
	
	public void addADGroup(long campaignId) throws ApiException, RemoteException{
	    AdGroup adGroup = new AdGroup();
	    adGroup.setName("test campaign group:" + System.currentTimeMillis());
	    adGroup.setStatus(AdGroupStatus.ENABLED);
	    adGroup.setCampaignId(campaignId);

	    TargetingSetting targeting = new TargetingSetting();
	    TargetingSettingDetail placements = new TargetingSettingDetail();
	    placements.setCriterionTypeGroup(CriterionTypeGroup.PLACEMENT);
	    placements.setTargetAll(Boolean.FALSE);

	    targeting.setDetails(new TargetingSettingDetail[] {placements});
	    adGroup.setSettings(new Setting[] {targeting});
	    AdGroupAdRotationMode rotationMode = new AdGroupAdRotationMode(AdRotationMode.OPTIMIZE);
	    adGroup.setAdGroupAdRotationMode(rotationMode);

	    /**
	     * 设置出价
	     */
	    BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
	    Money cpcBidMoney = new Money();
	    cpcBidMoney.setMicroAmount(1_000_000L);
	    CpcBid bid = new CpcBid();
	    bid.setBid(cpcBidMoney);
	    biddingStrategyConfiguration.setBids(new Bids[] {bid});
	    adGroup.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
	    
	    AdGroupOperation operation = new AdGroupOperation();
	    operation.setOperand(adGroup);
	    operation.setOperator(Operator.ADD);
	    AdGroupOperation[] operations = new AdGroupOperation[] {operation};
	    AdGroupReturnValue result = adGroupService.mutate(operations);
	}
	
	public List<AdGroup> listAdGroups(long campaignId) throws ApiException, RemoteException {
	  int offset = 0;
	  int PAGE_SIZE = 100;
      boolean morePages = true;

      // Create selector.
      SelectorBuilder builder = new SelectorBuilder();
      Selector selector = builder.fields(AdGroupField.Id, AdGroupField.Name)
                                 .orderAscBy(AdGroupField.Name)
                                 .offset(offset)
                                 .limit(PAGE_SIZE)
                                 .equals(AdGroupField.CampaignId, campaignId + "")
                                 .build();

      List<AdGroup> adGroupList = new ArrayList<>();
      while (morePages) {
        AdGroupPage page = adGroupService.get(selector);

        if (page.getEntries() != null) {
          adGroupList.addAll(Arrays.asList(page.getEntries()));
        }

        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
        morePages = offset < page.getTotalNumEntries();
      }
      return adGroupList;
	}
}
